import {
    BASE_URL
} from '../../http/config';

Page({

    data: {
        baseUrl: BASE_URL, // 用于 WXML 拼接图片地址
        icons: [{
                image: '/images/@2x_online.png',
                text: "在线课程",
                url: '/course/pages/SmartLiving/SmartLiving'
            },
            {
                image: '/images/@2x_teacher.png',
                text: "师资团队",
                url: '/course/pages/Faculty/Faculty'
            },
            {
                image: '/images/@2x_gonggao.png',
                text: "资讯公告",
                url: '/course/pages/ConsultNotice/ConsultNotice'
            },
        ],
        bodys: []
    },

    getCourse() {
        wx.request({
            url: `${BASE_URL}/course/all?pageNum=1&pageSize=8`,
            method: 'GET',
            success: (res) => {
                const list = res.data.data.list.map(item => ({
                    ...item,
                    // 预拼接图片地址，避免 WXML 中手动拼接
                    thumbnail: item.thumbnail 
                }))
                this.setData({
                    bodys: list
                })
                console.log("课程首页数据：", list);
            },
            fail: (err) => {
                console.error("请求失败：", err);
            }
        })
    },

    jumpCourse(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    },

    jumpCourseDetail(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/course/pages/courseDetail/courseDetail?id=${id}`
        });
    },

    onLoad(options) {
        this.getCourse();
    },

    onReady() {},

    onShow() {},

    onHide() {},

    onUnload() {},

    onPullDownRefresh() {},

    onReachBottom() {},

    onShareAppMessage() {}

})
